<template>
  <div class="app-container" ref="appcontainer">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="100px"
    >
      <el-form-item label="工程名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入工程名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="框架合同" prop="contract">
        <el-select
          v-model="queryParams.contract"
          placeholder="请选择框架合同"
          @change="onContract"
        >
          <el-option
            v-for="dict in contractOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictLabel"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="业主单位" prop="name">
        <el-input
          v-model="queryParams.enterpriseName"
          placeholder="请输入业主单位"
          clearable
          size="small"
        />
      </el-form-item>

      <el-form-item label="框架名称" prop="name">
        <el-input
          v-model="queryParams.subItem"
          placeholder="请输入框架名称"
          clearable
          size="small"
        />
      </el-form-item>

      <el-form-item label="状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择状态"
          clearable
          size="small"
        >
          <el-option
            v-for="dict in gc_engineering_status_Options"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5" v-show="roleId != 6">
        <el-button type="primary" plain icon="" size="mini" @click="handleAdd"
          >新增</el-button
        >
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :height="tableHeight"
      :data="enterpriseList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        type="selection"
        align="center"
        show-overflow-tooltip
      />
      <el-table-column label="序号" align="center" prop="id" />
      <el-table-column
        label="工程编号"
        align="center"
        prop="engineeringNumber"
      />
      <el-table-column
        label="工程名称"
        align="center"
        prop="name"
      />
      <el-table-column
        label="合同类型"
        align="center"
        prop="contract"
      />

      <el-table-column
        label="框架名称"
        align="center"
        prop="subItem"
      />
      <!-- <el-table-column label="工作量" align="center" prop="workload" width="200" /> -->
      <el-table-column
        label="业主单位"
        align="center"
        prop="enterpriseName"
      />
      <el-table-column label="状态" align="center" prop="status" >
        <template slot-scope="scope">
          <el-tag :type="scope.row.status == '2' ? 'success' : 'danger'">{{
            scope.row.status == "2" ? "完成" : "进行中"
          }}</el-tag>
        </template>
      </el-table-column>
      <!-- <el-table-column label="框架合同" align="center" prop="contract">
        <template slot-scope="scope">
          <dict-tag :options="contractOptions" :value="scope.row.contract" />
        </template>
      </el-table-column> -->
      <!-- <el-table-column label="项目类别" align="center" prop="type">
        <template slot-scope="scope">
          <dict-tag :options="typeOptions" :value="scope.row.type" />
        </template>
      </el-table-column> -->
      <!-- <el-table-column label="参建单位" align="center" width="240">
        <template slot-scope="scope">
          <ul style="padding:0;list-style:none;text-align:left;">
            <li>建设单位：{{ scope.row.constructionUnit }}</li>
            <li>监理单位：{{ scope.row.supervisionUnit }}</li>
            <li>设计单位：{{ scope.row.designUnit }}</li>
            <li>勘察单位：{{ scope.row.surveyUnit }}</li>
            <li>施工单位：{{ scope.row.construcUnit }}</li>
          </ul>
        </template>
      </el-table-column> -->
      <!-- <el-table-column label="项目经理" align="center" prop="pmName" />
      <el-table-column label="委托单" align="center" prop="commissionNum" />
      <el-table-column label="检测报告" align="center" prop="reportNum" /> -->
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        v-if="roleId != 6"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改工程信息对话框 -->
    <el-dialog
      :close-on-click-modal="false"
      :title="title"
      :visible.sync="open"
      width="500px"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="工程名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入工程名称" />
        </el-form-item>
        <el-form-item label="工程编号" prop="engineeringNumber">
          <el-input v-model="form.engineeringNumber" placeholder="" />
        </el-form-item>
        <el-form-item label="业主单位" prop="contract">
          <el-select v-model="form.enterpriseId" placeholder="请选择业主单位">
            <el-option
              v-for="item in enterpriseAll"
              :key="item.id"
              :label="item.name"
              value-key="item.id"
              :value="item.id"
            ></el-option>
          </el-select>
          <!-- <el-select v-model="form.contract" placeholder="请选择业主单位" @change="onContract">
            <el-option v-for="dict in contractOptions" :key="dict.dictValue" :label="dict.dictLabel"
              :value="dict.dictLabel"></el-option>
          </el-select> -->
        </el-form-item>
        <el-form-item label="合同类型" prop="contract">
          <el-select
            v-model="form.contract"
            placeholder="请选择框架合同"
            @change="onContract"
          >
            <el-option
              v-for="dict in contractOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictLabel"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="框架名称" prop="subItem" v-if="isShow">
          <el-input v-model="form.subItem" placeholder="" />
        </el-form-item>
        <el-form-item label="工作量" prop="workload">
          <el-input type="textarea" :rows="4" v-model="form.workload" placeholder="" />

        </el-form-item>

        <el-form-item v-if="isAdd == 2" label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in gc_engineering_status_Options"
              :key="dict.dictValue"
              :label="dict.dictValue * 1"
              >{{ dict.dictLabel }}</el-radio
            >
          </el-radio-group>
        </el-form-item>

        <!-- <el-form-item label="建设单位" prop="constructionUnit">
          <div style="display:flex;align-items: center;">
            <el-select v-model="form.constructionUnit" placeholder="请选择建设单位" @focus="getEnterpriseAll">
              <el-option
                v-for="dict in enterpriseAll"
                :key="dict.id"
                :label="dict.name"
                :value="dict.id"
              ></el-option>
            </el-select>
            <i class="el-icon-plus" style="margin:0 0 0 20px" @click="goto"></i>
          </div>
        </el-form-item> -->
        <!-- <el-form-item label="监理单位" prop="supervisionUnit">
          <div style="display:flex;align-items: center;">
            <el-select v-model="form.supervisionUnit" placeholder="请选择建设单位">
              <el-option
                v-for="dict in enterpriseAll"
                :key="dict.id"
                :label="dict.name"
                :value="dict.id"
              ></el-option>
            </el-select>
            <i class="el-icon-plus" style="margin:0 0 0 20px" @click="goto"></i>
          </div>
        </el-form-item> -->
        <!-- <el-form-item label="设计单位" prop="designUnit">
          <div style="display:flex;align-items: center;">
            <el-select v-model="form.designUnit" placeholder="请选择建设单位">
              <el-option
                v-for="dict in enterpriseAll"
                :key="dict.id"
                :label="dict.name"
                :value="dict.id"
              ></el-option>
            </el-select>
            <i class="el-icon-plus" style="margin:0 0 0 20px" @click="goto"></i>
          </div>
        </el-form-item> -->
        <!-- <el-form-item label="勘察单位" prop="surveyUnit">
          <div style="display:flex;align-items: center;">
            <el-select v-model="form.surveyUnit" placeholder="请选择建设单位">
              <el-option
                v-for="dict in enterpriseAll"
                :key="dict.id"
                :label="dict.name"
                :value="dict.id"
              ></el-option>
            </el-select>
            <i class="el-icon-plus" style="margin:0 0 0 20px" @click="goto"></i>
          </div>
        </el-form-item> -->
        <!-- <el-form-item label="施工单位" prop="construcUnit">
          <div style="display:flex;align-items: center;">
            <el-select v-model="form.construcUnit" placeholder="请选择建设单位">
              <el-option
                v-for="dict in enterpriseAll"
                :key="dict.id"
                :label="dict.name"
                :value="dict.id"
              ></el-option>
            </el-select>
            <i class="el-icon-plus" style="margin:0 0 0 20px" @click="goto"></i>
          </div>
        </el-form-item> -->
        <!-- <el-form-item label="项目经理" prop="projectManagerId">
          <el-select v-model="form.projectManagerId" placeholder="请选择项目经理" @focus="getPerson">
            <el-option
              v-for="dict in personList"
              :key="dict.id"
              :label="dict.name"
              :value="dict.id"
            ></el-option>
          </el-select>
        </el-form-item> -->
        <!-- <el-form-item label="项目类别" prop="type">
          <el-select v-model="form.type" placeholder="请选择项目类别">
            <el-option
              v-for="dict in typeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item> -->
        <!-- <el-form-item label="工程地点" prop="contacts">
          <el-input v-model="form.contacts" placeholder="请输入企业联系人" />
        </el-form-item>
        <el-form-item label="建设单位" prop="type">
          <el-select v-model="form.type" placeholder="请选择建设单位">
            <el-option
              v-for="dict in typeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item> -->
        <!-- <el-form-item label="监理单位" prop="type">
          <el-select v-model="form.type" placeholder="请选择监理单位">
            <el-option
              v-for="dict in typeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item> -->
        <!-- <el-form-item label="设计单位" prop="type">
          <el-select v-model="form.type" placeholder="请选择设计单位">
            <el-option
              v-for="dict in typeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="勘察单位" prop="type">
          <el-select v-model="form.type" placeholder="请选择勘察单位">
            <el-option
              v-for="dict in typeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item> -->
        <!-- <el-form-item label="施工单位" prop="type">
          <el-select v-model="form.type" placeholder="请选择施工单位">
            <el-option
              v-for="dict in typeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item> -->
        <!-- <el-form-item label="阶段" prop="stage">
          <el-select v-model="form.stage" placeholder="请选择阶段">
            <el-option
              v-for="dict in stageOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listEnterpriseAll,
  getEnterprise,
  logicDelEnterprise,
  addEnterprise,
  updateEnterprise,
  exportEnterprise,
} from "@/api/enterprise/enterprise";
import {
  getProject,
  listProject,
  addProject,
  updateProject,
  delProject,
  exportProject,
} from "@/api/project/project";
import { listPerson } from "@/api/staff";
import Cookies from "js-cookie";

export default {
  name: "Project",
  data() {
    return {
      isShow: false,
      isworkload: false,
      // 工程阶段
      stageOptions: [],
      // 框架合同
      contractOptions: [],
      // 所有人员
      personList: [],
      // 所有企业
      enterpriseAll: [],
      tableHeight: 0,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 企业信息表格数据
      enterpriseList: [],
      enterpriseNamearr: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 企业类型字典
      typeOptions: [],
      // 状态 1=开业，2=停业字典
      statusOptions: [],
      //状态 1=进行中，2=已完成
      gc_engineering_status_Options: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        code: null,
        type: null,
        status: null,
        subItem:null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        engineeringNumber: [
          {
            required: true,
            message: "编号不能为空",
            trigger: "blur",
          },
        ],
        workload: [
          {
            required: true,
            message: "工作量不能为空",
            trigger: "blur",
          },
        ],
        subItem: [
          {
            required: true,
            message: "框架名称不能为空",
            trigger: "blur",
          },
        ],
        name: [
          { required: true, message: "工程名称不能为空", trigger: "blur" },
        ],
        overview: [
          { required: true, message: "工程概况不能为空", trigger: "blur" },
        ],
        contract: [
          { required: true, message: "框架合同不能为空", trigger: "blur" },
        ],
      },
      roleId: null,
      roles: [],
      enterpriseId: null,
      isAdd:1,
    };
  },
  created() {
    //当前用户的信息
    let userInfo = JSON.parse(Cookies.get("userinfo"));
    this.roles = userInfo.roles;
    this.roleId = userInfo.roleId;
    this.enterpriseId = userInfo.enterpriseId;

    this.getList();
    this.calHeight();
    this.getDicts("gc_engineering_ht").then((response) => {
      this.contractOptions = response.data;
    });
    this.getDicts("gc_engineering_type").then((response) => {
      this.typeOptions = response.data;
    });
    this.getDicts("gc_engineering_stage").then((response) => {
      this.stageOptions = response.data;
    });

    this.getDicts("gc_engineering_status").then((response) => {
      this.gc_engineering_status_Options = response.data;
    });

    this.getEnterpriseAll();
  },
  watch: {
    showSearch: "calHeight",
  },
  methods: {
    //企业信息查询所有
    // getgclist() {
    //   listEnterpriseAll().then(res => {
    //     this.enterpriseNamearr = res.data
    //   })
    // },
    onContract(val) {
      if (val == "框架合同") {
        this.isShow = true;
      } else {
        //this.form.subItem = ''
        this.isShow = false;
      }
    },
    // 获取人员信息
    getPerson() {
      listPerson({ roleId: 4 }).then((res) => {
        this.personList = res.data;
      });
    },
    getEnterpriseAll() {
      listEnterpriseAll().then((res) => {
        this.enterpriseAll = res.data;
      });
    },
    // 跳转
    goto() {
      this.open = false;
      this.$router.push("/basics/basics/enterprise");
    },
    calHeight() {
      this.$nextTick(() => {
        const rect = this.$refs.appcontainer.getBoundingClientRect();
        let height = this.$refs.queryForm.$el.offsetHeight;
        this.tableHeight = rect.height - height - 120;
      });
    },
    /** 查询列表 */
    getList() {
      // for (let i = 0; i < this.roles.length; i++) {
      //   if (this.roles[i].roleId == 6 || this.roles[i].roleId == 9) {
      //     this.queryParams.enterpriseId = this.enterpriseId;
      //     break;
      //   }
      // }

      this.queryParams.isRoleShow = 1;
      this.loading = true;
      listProject(this.queryParams).then((response) => {
        this.enterpriseList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        code: null,
        type: null,
        enterpriseId: null,
        contacts: null,
        mobile: null,
        status: 0,
        createTime: null,
        updateTime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.showSearch = true;
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams={
        pageNum: 1,
        pageSize: 10,
        name: null,
        code: null,
        type: null,
        status: null,
        subItem:null,
      },
      this.resetForm("queryParams");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加工程信息";
      this.isAdd = 1;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      this.isAdd = 2;
      getProject(id).then((response) => {
        let data = response.data;
        if (data.contract == "框架合同") {
          this.isShow = true;
        } else {
          this.isShow = false;
        }
        this.form = data;
        this.open = true;
        this.title = "修改工程信息";
        
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateProject(this.form).then((response) => {
              if(response.data == 2){
                this.msgError("尚有未到完成度100%的进度，无法完成");
              }else if(response.data == 3){
                this.msgError("此工程没有进度，无法完成");
              }else{
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addProject(this.form).then((response) => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm("是否确认删除?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return delProject(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const ids = this.ids;
      console.log(ids);
      this.queryParams.ids = ids;
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出工程信息数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.exportLoading = true;
          return exportProject(queryParams);
        })
        .then((response) => {
          this.download(response.msg);
          this.exportLoading = false;
        })
        .catch(() => {});
    },
  },
};
</script>

<style>
.w {
  display: flex;
  align-items: center;
}
</style>
